package com.lt.contract.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lt.contract.bean.utils.ExcelImport;
import com.lt.contract.dao.NmBoutiqueNetworkDao;
import com.lt.contract.entity.NmBoutiqueNetwork;
import com.lt.contract.service.NmBoutiqueNetworkService;
import com.lt.report.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 政企精品网(NmBoutiqueNetwork)表服务实现类
 *
 * @author makejava
 * @since 2020-03-31 21:14:35
 */
@Service("nmBoutiqueNetworkService")
@Slf4j
public class NmBoutiqueNetworkServiceImpl implements NmBoutiqueNetworkService {
    @Resource
    private NmBoutiqueNetworkDao nmBoutiqueNetworkDao;
    @Autowired
    AsyncService asyncService;

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public NmBoutiqueNetwork queryById() {
        return this.nmBoutiqueNetworkDao.queryById();
    }

    @Override
    public List<NmBoutiqueNetwork> queryAll(Map<String, Object> map) {
        return nmBoutiqueNetworkDao.queryAll(map);
    }

    @Override
    public Long queryCount(Map<String, Object> map) {
        return nmBoutiqueNetworkDao.queryCount(map);
    }

    @Override
    public List<Map<String, Object>> queryRepetition(List<String> list, String cycleId) {
        return nmBoutiqueNetworkDao.queryRepetition(list, cycleId);
    }

    @Override
    public String getBankListByExcel(InputStream in, String fileName, String StaffId) throws Exception {
        //选择文件中的列
        String tableName = "";
        if (fileName.contains("精品网")) {
            tableName = "boutiqueNetwork";
        } else {
            log.error("上传文件不符合规定");
            return "请上次相关文件";
        }
        //创建Excel工作薄
        Workbook work = ExcelImport.getWorkbook(in, fileName);
        Sheet sheet = work.getSheetAt(0);//只取第一个Sheet
        List<String> isRecur = ExcelImport.isRecur(sheet, 3);
        if (isRecur.size() > 0) {
            JSONObject object = new JSONObject();
            object.put("您上传的业务号码有重复，请确认！", isRecur);
            return JSON.toJSONString(object);
        }
        List<List<String>> list = ExcelImport.queryRepetition(sheet, 3);//第3列是计费号码
        List<Map<String, Object>> list1 = new ArrayList<>();
        String cycleIdNew = ExcelImport.getCellValue(sheet.getRow(2).getCell(8));//获取上传文件中的账期
        for (int i = 0; i < list.size(); i++) {
            list1.addAll(nmBoutiqueNetworkDao.queryRepetition(list.get(i), cycleIdNew));
        }
        if (list1.size() > 0) {
            return JSON.toJSONString(list1);
        }
        List<List<NmBoutiqueNetwork>> lists = listNmBoutiqueNetwork(sheet, StaffId);
        int size = lists.size();
        for (int i = 0; i < size; i++) {
            List<NmBoutiqueNetwork> list2 = lists.get(i);
            asyncService.executeBoutigue(list2);
        }
        return "上传成功";
    }

    /**
     * 导入精品网业务
     *
     * @param sheets 导入精品网业务
     * @return
     */
    public List<List<NmBoutiqueNetwork>> listNmBoutiqueNetwork(Sheet sheets, String StaffId) throws Exception {
        List<List<NmBoutiqueNetwork>> lists = new ArrayList<>();
        List<NmBoutiqueNetwork> list = new ArrayList<>();
        Cell cell = null;
        for (int j = sheets.getFirstRowNum() + 1; j <= sheets.getLastRowNum() + 1; j++) {
            Row row = sheets.getRow(j);
            int flag = 200;
            if (list.size() != 0 && list.size() % flag == 0 || j == sheets.getLastRowNum() + 1) {
                List<NmBoutiqueNetwork> copy = new ArrayList<>(list);
                lists.add(copy);
                list.clear();
            }
            if (row == null || row.getFirstCellNum() == j) {
                continue;
            } else if (ExcelImport.isAllRowEmpty(row, row)) {
                continue;
            }
            NmBoutiqueNetwork nmBoutiqueNetwork = new NmBoutiqueNetwork();
            for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                cell = row.getCell(y);
                if (cell == null || cell.toString().equals("")) {
                    continue;
                }
                switch (y + 1) {
                    case 1:
                        nmBoutiqueNetwork.setProvince(ExcelImport.getCellValue(cell));
                        break;
                    case 2:
                        nmBoutiqueNetwork.setCity(ExcelImport.getCellValue(cell));
                        break;
                    case 3:
                        nmBoutiqueNetwork.setCityName(ExcelImport.getCellValue(cell));
                        break;
                    case 4:
                        nmBoutiqueNetwork.setSerialNumber(ExcelImport.getCellValue(cell));
                        break;
                    case 5:
                        nmBoutiqueNetwork.setProductId(ExcelImport.getCellValue(cell));
                        break;
                    case 6:
                        nmBoutiqueNetwork.setItem(ExcelImport.getCellValue(cell));
                        break;
                    case 7:
                        nmBoutiqueNetwork.setOtherCity(ExcelImport.getCellValue(cell));
                        break;
                    case 8:
                        nmBoutiqueNetwork.setOtherProvince(ExcelImport.getCellValue(cell));
                        break;
                    case 9:
                        nmBoutiqueNetwork.setCycleId(ExcelImport.getCellValue(cell));
                        break;
                    case 10:
                        nmBoutiqueNetwork.setHAmount(ExcelImport.getCellValue(cell));
                        break;
                    case 11:
                        nmBoutiqueNetwork.setAZ(ExcelImport.getCellValue(cell));
                        break;
                    case 12:
                        nmBoutiqueNetwork.setHAmountF(ExcelImport.getCellValue(cell));
                        break;
                    case 13:
                        nmBoutiqueNetwork.setHProportion(ExcelImport.getCellValue(cell));
                        break;
                    case 14:
                        nmBoutiqueNetwork.setCustName(ExcelImport.getCellValue(cell));
                        break;
                    case 15:
                        nmBoutiqueNetwork.setIsReconciliation(ExcelImport.getCellValue(cell));
                        break;
                    case 16:
                        nmBoutiqueNetwork.setTrellisCode(ExcelImport.getCellValue(cell));
                        break;
                    case 17:
                        nmBoutiqueNetwork.setTrellisName(ExcelImport.getCellValue(cell));
                        break;
                    case 18:
                        nmBoutiqueNetwork.setYzStaffid(ExcelImport.getCellValue(cell));
                        break;
                    case 19:
                        nmBoutiqueNetwork.setAccountManager(ExcelImport.getCellValue(cell));
                        break;
                    case 20:
                        nmBoutiqueNetwork.setIncome(ExcelImport.getCellValue(cell));
                        break;

                }
            }
            nmBoutiqueNetwork.setInputUser(StaffId);
            nmBoutiqueNetwork.setUpdateUser(StaffId);
            nmBoutiqueNetwork.setDataTag("0");
            list.add(nmBoutiqueNetwork);

        }
        return lists;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<NmBoutiqueNetwork> queryAllByLimit(int offset, int limit) {
        return this.nmBoutiqueNetworkDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param nmBoutiqueNetwork 实例对象
     * @return 实例对象
     */
    @Override
    public NmBoutiqueNetwork insert(NmBoutiqueNetwork nmBoutiqueNetwork) {
        this.nmBoutiqueNetworkDao.insert(nmBoutiqueNetwork);
        return nmBoutiqueNetwork;
    }

    /**
     * 修改数据
     *
     * @param nmBoutiqueNetwork 实例对象
     * @return 实例对象
     */
    @Override
    public NmBoutiqueNetwork update(NmBoutiqueNetwork nmBoutiqueNetwork) {
        this.nmBoutiqueNetworkDao.update(nmBoutiqueNetwork);
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @Override
    public boolean deleteById() {
        return this.nmBoutiqueNetworkDao.deleteById() > 0;
    }
}
